package com.lxl.xm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxl.xm.common.DailyRegistration;
import com.lxl.xm.model.dto.student.ValidateStudent;
import com.lxl.xm.model.entity.Student;
import com.lxl.xm.service.StudentService;
import com.lxl.xm.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
* @author 86135
* @description 针对表【student(`student`)】的数据库操作Service实现
* @createDate 2024-01-31 16:32:42
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    @Resource
    StudentMapper studentMapper;

    @Override
    public List<DailyRegistration> getRegisteredStudentsLast7Days() {
//        List<DailyRegistration> result = new ArrayList<>();
//
//        Calendar calendar = Calendar.getInstance();
//        // 结束日期为当前日期
//        Date endDate = calendar.getTime();
//        calendar.add(Calendar.DAY_OF_YEAR, -7);
//        //7天前的日期
//        Date startDate = calendar.getTime();
//
//        // 查询每天的注册人数
//        for (int i = 0; i < 7; i++) {
//            Date currentDate = calendar.getTime();
//            LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            studentLambdaQueryWrapper.eq(Student::getIsRegister,1)
//                    .ge(Student::getRegisterDate,startDate);
//            Long count = studentMapper.selectCount(studentLambdaQueryWrapper);
//            result.add(new DailyRegistration(currentDate,count));
//
//            // 将日期往后推一天
//            calendar.add(Calendar.DAY_OF_YEAR, 1);
//            startDate = calendar.getTime();
//        }

        List<DailyRegistration> result = new ArrayList<>();

        // 获取近七天的日期范围
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date()); // 设置当前日期
        calendar.add(Calendar.DAY_OF_YEAR, -6); // 将当前日期往前推六天，使其为七天前的日期
        Date startDate = calendar.getTime(); // 开始日期为七天前的日期

        // 查询每天的注册人数
        for (int i = 0; i < 7; i++) {
            LambdaQueryWrapper<Student> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(Student::getIsRegister, 1)
                    .ge(Student::getRegisterDate, startDate)
                    .lt(Student::getRegisterDate, addDays(startDate, 1)); // 查询指定日期当天注册的学生
            long count = studentMapper.selectCount(wrapper);
            result.add(new DailyRegistration(startDate, count));
            // 将日期往后推一天
            calendar.add(Calendar.DAY_OF_YEAR, 1);
            startDate = calendar.getTime();
        }
        return result;

//        List<DailyRegistration> result = new ArrayList<>();
//
//        // 获取当前日期的起始时间
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date()); // 设置当前日期
//        calendar.set(Calendar.HOUR_OF_DAY, 0);
//        calendar.set(Calendar.MINUTE, 0);
//        calendar.set(Calendar.SECOND, 0);
//        calendar.set(Calendar.MILLISECOND, 0);
//
//        // 统计近七天的注册人数
//        for (int i = 0; i < 7; i++) {
//            Date startDate = calendar.getTime(); // 当天的起始时间
//            calendar.add(Calendar.DAY_OF_YEAR, 1); // 将日期往后推一天
//            Date endDate = calendar.getTime(); // 当天的结束时间
//
//            LambdaQueryWrapper<Student> wrapper = Wrappers.lambdaQuery();
//            wrapper.eq(Student::getIsRegister, 1)
//                    .ge(Student::getRegisterDate, startDate)
//                   .lt(Student::getRegisterDate, addDays(startDate, 1)); // 查询指定日期当天注册的学生
//           long count = studentMapper.selectCount(wrapper);
//            result.add(new DailyRegistration(startDate, count));
//        }
//
//        return result;
    }

    private Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, days);
        return calendar.getTime();
    }

    @Override
    public Long getTotalRegisteredStudents() {
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(Student::getIsRegister,1);
        return studentMapper.selectCount(studentLambdaQueryWrapper);
    }

    @Override
    public List<Map<String, Object>> countStudentsByProvince() {
        return studentMapper.countStudentsByProvince();
    }

    @Override
    public List<Map<String, Object>> countStudentsByCollege() {
        return studentMapper.countStudentsByCollege();
    }

    /**
     * 验证学生信息是否存在数据库中
     * @param validateStudent
     * @return
     */
    @Override
    public Boolean validateStudentByIdCardAndNameAndNoticeNumber(ValidateStudent validateStudent) {
        String name = validateStudent.getName();
        String identityCardNumber = validateStudent.getIdentityCardNumber();
        String admissionNoticeNumber = validateStudent.getAdmissionNoticeNumber();
        //查询学生是否存在并且没有注册过
        LambdaQueryWrapper<Student> studentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        studentLambdaQueryWrapper.eq(Student::getName,name)
                .eq(Student::getAdmissionNoticeNumber,admissionNoticeNumber)
                .eq(Student::getIdentityCardNumber,identityCardNumber)
                .eq(Student::getIsRegister,0);
        return this.getOne(studentLambdaQueryWrapper) != null;
    }
}




